package com.example.healkep.View.health;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.example.healkep.R;
import com.example.healkep.View.health.helper.healthHelper;
import com.example.healkep.adapter.diseaseItemAdapter;
import com.example.healkep.bean.globalUserInfo;
import com.huawei.hms.utils.HEX;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class healthActivity extends AppCompatActivity implements View.OnClickListener,View.OnLongClickListener {
    private SearchView searchView;
    private RecyclerView show,edit;
    private diseaseItemAdapter showAdapter,editAdapter;
    private List<String> showDis = new ArrayList<>(),editDis = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health2);
        initComponent();
    }
    private void initComponent(){
        searchView = findViewById(R.id.search);
        show = findViewById(R.id.rcv_show);
        edit = findViewById(R.id.rcv_edit);
        showAdapter = new diseaseItemAdapter(this, null,this, showDis);
        editAdapter = new diseaseItemAdapter(this,this,null,editDis);
        show.setLayoutManager(new LinearLayoutManager(this));
        show.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
        edit.setLayoutManager(new LinearLayoutManager(this));
        edit.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
        show.setAdapter(showAdapter);
        edit.setAdapter(editAdapter);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                healthHelper helper = new healthHelper(new EditHandler());
                helper.getDataByStr(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                healthHelper helper = new healthHelper(new EditHandler());
                helper.getDataByStr(newText);
                return false;
            }
        });

        healthHelper helper = new healthHelper(new ShowHandler());
        helper.getUserDiseases(globalUserInfo.getInstance().getUsername());
        helper = new healthHelper(new EditHandler());
        helper.getDataByStr("");
    }

    @Override
    public void onClick(View v) {
        TextView textView = (TextView) v;
        final String value = textView.getText().toString();
        new AlertDialog.Builder(this)
                        .setTitle("提示")
                        .setMessage("是否确认添加？")
                        .setPositiveButton("确认",(d,which)->{
                            StringBuilder builder = new StringBuilder();
                            for(String i : showDis){
                                builder.append(i).append(";");
                            }
                            builder.append(value).append(";");
                            healthHelper helper = new healthHelper(new UpdateHandler());
                            helper.updateDisease(globalUserInfo.getInstance().getUsername(), builder.toString());
                        }).setNegativeButton("取消",(d,which)->{
                        }).create().show();
    }

    @Override
    public boolean onLongClick(View v) {
        TextView textView = (TextView) v;
        final String value = textView.getText().toString();
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("是否确认删除？")
                .setPositiveButton("确认",(d,which)->{
                    StringBuilder builder = new StringBuilder();
                    showDis.remove(value);
                    for(String i : showDis){
                        builder.append(i).append(";");
                    }

                    healthHelper helper = new healthHelper(new UpdateHandler());
                    helper.updateDisease(globalUserInfo.getInstance().getUsername(), builder.toString());
                }).setNegativeButton("取消",(d,which)->{
                }).create().show();
        return false;
    }
    class ShowHandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what == healthHelper.SUCCESS){
                Bundle bundle = msg.getData();
                String dis = bundle.getString("disease");
                String[] strs = dis.split(";");
                showDis.clear();
                showDis.addAll(Arrays.asList(strs));
                showAdapter.update(showDis);
            }else if(msg.what == healthHelper.CONNECT_FAILED){
                Toast.makeText(healthActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(healthActivity.this,"未知错误",Toast.LENGTH_SHORT).show();
            }

        }
    }
    class EditHandler extends  Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what == healthHelper.SUCCESS){
                Bundle bundle = msg.getData();
                editDis = bundle.getStringArrayList("diseases");
                if(editDis == null) editDis = new ArrayList<>();
                editAdapter.update(editDis);
            }else if(msg.what == healthHelper.CONNECT_FAILED){
                Toast.makeText(healthActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(healthActivity.this,"未知错误",Toast.LENGTH_SHORT).show();
            }
        }
    }
    class UpdateHandler extends Handler{
        public void handleMessage(@NonNull Message msg) {
            if(msg.what == healthHelper.SUCCESS){
                Bundle bundle = msg.getData();
                String dis = bundle.getString("disease");
                String[] strs = dis.split(";");
                showDis.clear();
                showDis.addAll(Arrays.asList(strs));
                showAdapter.update(showDis);
                Toast.makeText(healthActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
            }else if(msg.what == healthHelper.CONNECT_FAILED){
                Toast.makeText(healthActivity.this,"网络异常",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(healthActivity.this,"未知错误",Toast.LENGTH_SHORT).show();
            }
        }
    }
}